package com.ljs.manager
{
	import com.ljs.net.Packet;
	import com.ljs.net.login.LogInData;
	import com.ljs.net.opcodes.Opcodes;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	
	public class NetManager extends EventDispatcher
	{
		private var _socket:Socket;
		private var _length:int=0;
		private var _inputStream:ByteArray;
		private var _packetArray:Array=[];
		public function NetManager()
		{
			init();
		}
		
		private function init()
		{
			_socket=new Socket();
			_socket.addEventListener(Event.CONNECT,onConnect);
			_socket.addEventListener(Event.CLOSE,onClose);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA,onSocketData);
			_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_socket.connect("192.168.1.10",1234);

		}
		
		protected function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("socket IO错误！",event.text);
		}
		
		protected function securityErrorHandler(event:SecurityErrorEvent):void
		{
			trace("socket安全错误！",event.text);
		}
		
		public function sendPacket(p:Packet)
		{
			if(p)
			{
				var byteArray1:ByteArray=new ByteArray;
				p.getMessage().writeTo(byteArray1);
				var length:int=byteArray1.length;
				trace("byteArray1",byteArray1.length)
				var byteArray:ByteArray=new ByteArray;
				byteArray.writeShort(length);
				byteArray.writeByte(0);
				byteArray.writeByte(p.getPacketID());
				byteArray.writeBytes(byteArray1);
			
				_socket.writeBytes(byteArray);
				_socket.flush();
			}
		}
		
		private function onConnect(e:Event)
		{
			trace("连接成功！");
		}
		
		private function onClose(e:Event)
		{
			trace("close")
		}
		
		private function onSocketData(e:ProgressEvent)
		{
			do
			{
				// 如果当前包长等于0，说明上个包已经读完，重新获得新包的长度
				if ( _length == 0 )
				{
					if ( _socket.bytesAvailable < 2 )
					{
						break;
					}
					else
					{
						_length = _socket.readShort()+2;
					}
				}
				// 如果包长小于缓冲区可读数据长度，说明包数据还没完全发过来，等发过来再继续读。
				if ( _length > _socket.bytesAvailable) 
				{
					break;
				}
				_socket.readByte();//这个数据还没用到
				var opcode:int 		= _socket.readByte();//读取包类型
				var bytes:ByteArray = getInputStream();
				// 读取指定长度的缓冲区数据
				if ( _length> 0 )
				{
					_socket.readBytes(bytes, 0, _length-2);
					//解包
					switch(opcode)
					{
						case Opcodes.LOGIN:
							var pkt:LogInData=new LogInData();
							pkt.mergeFrom(bytes);
							trace(pkt.userName,pkt.password);	
							break;
					}
				}
				_length = 0;
			}
			while (_socket.bytesAvailable > 4);
		}
		
		private function getInputStream():ByteArray
		{
			if( _inputStream == null)
			{
				_inputStream = new ByteArray;	
			}
			else 
			{
				_inputStream.clear();
			}	
			return _inputStream;
		}
		
		public function packetToDataPool()
		{
			var length:int  	= _packetArray.length;
			var i:int 	= 0;
			for( i=0; i < length; i++ )
			{
				_packetArray[i].ToDataPool();
			}	
			_packetArray = [];
		}
	}
}